package com.bright.ghj.overt.web.controller;

import com.bright.ghj.common.pojo.vo.PageVO;
import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.ghj.overt.util.SecurityUtil;
import com.bright.ghj.overt.constant.DataConstant;
import com.bright.ghj.overt.constant.PublicConstant;
import com.bright.ghj.overt.enums.NavigateItem;
import com.bright.ghj.overt.pojo.dto.UserDTO;
import com.bright.ghj.overt.pojo.query.StatByZtQuery;
import com.bright.ghj.overt.pojo.vo.StatByZtDataVO;
import com.bright.ghj.overt.pojo.vo.StatByZtDetailDataVO;
import com.bright.ghj.overt.pojo.vo.StatInfoVO;
import com.bright.ghj.overt.pojo.vo.TableTypeVO;
import com.bright.ghj.overt.service.NavigateService;
import com.bright.ghj.overt.service.StatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * @Author hxj
 * @Date 2024/1/24 17:20
 * @Description 统计表
 */
@RequestMapping("/stat")
@RestController
@RequiredArgsConstructor
@Api(tags = "统计接口")
public class StatController {

    private final StatService statService;
    private final NavigateService navigateService;
    private final DataConstant dataConstant;

    /**
     * 获取公开情况统计表的信息 包括表名 表头 查询条件等
     * @return 统计表信息对象
     */
    @ApiOperation(value = "公开情况统计表信息")
    @GetMapping("/statByZt/info/{ywType}")
    public RestResult statByZtInfo(@PathVariable("ywType") String ywTypeString) {
        int ywType = Integer.parseInt(ywTypeString);

        StatInfoVO statInfoVO = new StatInfoVO();
        // 统计表标题 财务/党务/事务+公开情况统计
        String title = PublicConstant.GK_TYPE_MAP.getOrDefault(ywType, "财务") + NavigateItem.STAT_BY_ZT.getDescription();
        statInfoVO.setTitle(title);
        statInfoVO.setHeaders(statService.listStatByZtTableHeaders());
        statInfoVO.setParams(statService.listStatByZtParams());
        statInfoVO.setTableTypes(getTableTypes(ywType));
        return RestResult.success(statInfoVO);
    }

    /**
     * 分页查询公开情况统计表的数据
     * @return 分页对象
     */
    @PreAuthorize("hasAnyAuthority('statZt') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "公开情况统计表分页查询")
    @PostMapping("/statByZt/page")
    public RestResult pageStatByZtData(@RequestBody StatByZtQuery query) {
        putUserInfoIntoQuery(query);
        // 20240520 当公开开始结束时间为空时 默认查上一月份的数据 避免数据量过大查询卡慢
        putDefaultDateIntoQuery(query);

        PageVO<StatByZtDataVO> pageVO = statService.pageStatByZtData(query);
        return RestResult.success(pageVO);
    }

    private void putDefaultDateIntoQuery(StatByZtQuery query) {
        // 取上一月份的1日
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, -1);

        if (query.getPublicDateBegin() == null) {
            query.setPublicDateBegin(cal.getTime());
        }
        cal.set(Calendar.DATE, 28);
        if (query.getPublicDateEnd() == null) {
            query.setPublicDateEnd(cal.getTime());
        }
    }

    private void putUserInfoIntoQuery(StatByZtQuery query) {
//        UserDTO loginUser = navigateService.getLoginUser();
        UserDTO loginUser = SecurityUtil.getLoginUser();
        query.setUserDistNo(Arrays.asList(loginUser.getDistNoList()));
        query.setUserZtId(Arrays.asList(loginUser.getZtIdList()));
    }

    /**
     * 获取公开情况统计表二级明细表的信息 包括表名 表头 查询条件等
     * @return 统计表信息对象
     */
    @ApiOperation(value = "二级明细统计表信息")
    @GetMapping("/statByZtDetail/info/{ywType}")
    public RestResult statByZtDetailInfo(@PathVariable("ywType") String ywTypeString) {
        int ywType = Integer.parseInt(ywTypeString);

        StatInfoVO statInfoVO = new StatInfoVO();
        statInfoVO.setTitle("报表数据明细");
        statInfoVO.setHeaders(statService.listStatByZtDetailTableHeaders());
        // 二级明细不显示查询参数
//        statInfoVO.setParams(listStatByZtParams(query.getYwType()));
//        statInfoVO.setTableTypes(getTableTypes(query.getYwType()));
        statInfoVO.setParams(new ArrayList<>());
        statInfoVO.setTableTypes(new ArrayList<>());
        return RestResult.success(statInfoVO);
    }

    /**
     * 分页查询公开情况统计表的数据
     * @return 分页对象
     */
    @PreAuthorize("hasAnyAuthority('statZt') && hasAnyAuthority('isPermitAdmin')")
    @ApiOperation(value = "二级明细统计表分页查询")
    @PostMapping("/statByZtDetail/page")
    public RestResult pageStatByZtDetailData(@RequestBody StatByZtQuery query) {
        putUserInfoIntoQuery(query);
        // 20240520 当公开开始结束时间为空时 默认查上一月份的数据 避免数据量过大查询卡慢
        putDefaultDateIntoQuery(query);
        if (query.getDetailColumn() == null) {
            return RestResult.fail("detailColumn不能为空！");
        }
        PageVO<StatByZtDetailDataVO> pageVO = statService.pageStatByZtDetailData(query);
        return RestResult.success(pageVO);
    }


//    private List<StatParamVO> listStatByZtParams(Integer ywType) {
//        return statService.listStatByZtParams();
//    }

    /**
     * 获取公开主表查询的信息 包括表名 表头 查询条件等
     * @return 统计表信息对象
     */
    @ApiOperation(value = "公开主表信息")
    @GetMapping("/openReportRecord/info/{ywType}")
    public RestResult openReportRecordInfo(@PathVariable("ywType") String ywTypeString) {
        Integer ywType = Integer.parseInt(ywTypeString);
        StatInfoVO statInfoVO = new StatInfoVO();
        // 统计表标题 财务/党务/事务+公开情况统计
        String title = PublicConstant.GK_TYPE_MAP.getOrDefault(ywType, "财务") + "数据录入";
        statInfoVO.setTitle(title);
        statInfoVO.setHeaders(statService.listOpenReportRecordTableHeaders());
        statInfoVO.setParams(statService.listOpenReportRecordParams());
        statInfoVO.setTableTypes(getTableTypes(ywType));
        return RestResult.success(statInfoVO);
    }

    private List<TableTypeVO> getTableTypes(Integer ywType) {
        String category;
        if (ywType == PublicConstant.GK_TYPE_DW) {
            category = dataConstant.DW_CATEGORY;
        } else if (ywType == PublicConstant.GK_TYPE_SW) {
            category = dataConstant.SW_CATEGORY;
        } else {
            category = dataConstant.CW_CATEGORY;
        }
        return navigateService.listTableTypesByMenuId(category);
    }


}
